<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 检测数据管理
 */
namespace app\index\service;

use app\common\JwtToken;
use app\common\model\DetectionModel;
use app\common\model\ProjectModel;
use think\Exception;

class DetectionService
{
    protected static $detectionModel;
    /**
     * UserService constructor.
     * @param DetectionModel $detectionModel
     * @author zws
     */
    public function __construct(DetectionModel $detectionModel)
    {
        self::$detectionModel = $detectionModel;
    }

    /**
     * 上传检测数据
     * @param $param
     * @return bool
     * @author zws
     */
    public function uploadData($param)
    {
        if(empty($param))
        {
            app_fail(9997);
        }
//        if(!isset($param['paramter']) || empty($param['paramter']))
//        {
//            app_fail(6995);
//        }
        if(!isset($param['message']) || empty($param['message']))
        {
            app_fail(6994);
        }
        if(!isset($param['order_id']) || empty($param['order_id']))
        {
            app_fail(6996);
        }
        if(!isset($param['project_id']) || empty($param['project_id']))
        {
            app_fail(6993);
        }
        if(!isset($param['status']) || empty($param['status']))
        {
            app_fail(6992);
        }

        try {
//            self::$detectionModel->allowField(true)->save($param);
            $data['project_id'] = $param['project_id'];
            $data['order_id'] = $param['order_id'];
            $data['user_id'] = $param['user_id'];
            $data['paramter'] = xml_parser($param['paramter']) ? json_encode(simplexml_load_string($param['paramter'])) : $param['paramter'];
            $data['message'] = $param['message'];
            $data['wave_form'] = $param['wave_form'];
            $data['status'] = $param['status'];

            self::$detectionModel->allowField(true)->save($data);
            return true;
        } catch (Exception $e) {
            app_fail(9998);
        }
    }

    /**
     * 检测数据
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @author zws
     */
    public function detectionData($param)
    {
        if(empty($param))
        {
            app_fail(9997);
        }
        if(!isset($param['order_id']) || empty($param['order_id']))
        {
            app_fail(6996);//工单
        }
        $user_id = $param['userId'];
        $where['order_id'] = $param['order_id'];
        $where['user_id'] = $user_id;
        if(isset($param['project_name']) && !empty($param['project_name']))
        {
            $projectModel = new ProjectModel();
            $projectData = $projectModel->where("project_name", "like", "%".$param['project_name']."%")->find();
            if(empty($projectData)){
                return [];
            }
            $where['project_id'] = $projectData['id'];
        }
        $result = self::$detectionModel->where($where)
            ->order("create_time", "desc")
            ->limit(5)
            ->select();
        $detection_status = config('param_config.detection_status');
        $data = [];
        if($result)
        {
            $result->map(function (&$item) {
                $item->project_name = $item->project->project_name;
            });
            foreach ($result as $k=>$v)
            {
                $data[$k]['project_name'] = $v->project_name;
                $data[$k]['paramter'] = $v->paramter;
                $data[$k]['message'] = $v->message;
                $data[$k]['wave_form'] = $v->wave_form;
                $data[$k]['create_time'] = $v->create_time;
                $data[$k]['status'] = isset($detection_status[$v->status]) ?
                    $detection_status[$v->status] : '';
            }
        }
        return $data;
    }

}